package service

import (
	"gin-rkboot-sw/internal/global"
	"gin-rkboot-sw/internal/model/entity"
	"github.com/samber/mo"
)

var (
	User = sUser{}
)

type sUser struct{}

func (u *sUser) List() mo.Result[[]entity.User] {
	var users []entity.User
	return mo.TupleToResult(users, global.DB.Find(&users).Error)
}

func (u *sUser) Detail(id string) mo.Result[entity.User] {
	var user entity.User
	return mo.TupleToResult(user, global.DB.Where(&entity.User{Id: id}).Find(&user).Error)
}

func (u *sUser) Save(user entity.User) mo.Result[any] {
	return mo.TupleToResult[any](nil, global.DB.Create(&user).Error)
}

func (u *sUser) Update(user entity.User) mo.Result[any] {
	return mo.TupleToResult[any](nil, global.DB.Save(&user).Error)
}

func (u *sUser) Delete(id string) mo.Result[any] {
	return mo.TupleToResult[any](nil, global.DB.Where(&entity.User{Id: id}).Delete(entity.User{}).Error)
}
